//
//  MeUpTableViewCell.m
//  JJHW_coreData
//
//  Created by lizhenqiang on 16/5/22.
//  Copyright © 2016年 HFKJ_LZQ. All rights reserved.
//

#import "MeUpTableViewCell.h"
#import "Helper.h"
#import "LoginHandle.h"
@interface MeUpTableViewCell ()<UIImagePickerControllerDelegate>

@property(assign, nonatomic)int uploadtag;
@end

@implementation MeUpTableViewCell

- (void)awakeFromNib {
    // Initialization code
    _mywidth = DeviceWidth;
    _myheight = kScaleFrom_iphone6_Desgin(290)+60;
    self.frame = CGRectMake(0, 0, _mywidth, _myheight);
    self.bgImageView.frame = CGRectMake(0, 0, _mywidth, kScaleFrom_iphone6_Desgin(290));
    
    
    CGFloat startHeight = (kScaleFrom_iphone6_Desgin(290)-(43+19+70+9+21+8+30))/2.0;
    CGFloat textstartWidth = (_mywidth-144)/2.0;
    _textImageView.frame = CGRectMake(textstartWidth, startHeight, 144, 43);
    CGFloat headBtnstartWidth = (_mywidth-70)/2.0;
    _userHeadBtn.frame = CGRectMake(headBtnstartWidth, _textImageView.frame.origin.y+_textImageView.frame.size.height+19, 70, 70);
    _nickNameLabel.frame = CGRectMake(0, _userHeadBtn.frame.origin.y+_userHeadBtn.frame.size.height+9, _mywidth, 21);
    CGFloat signLabelWidth = 60;
    _signLabel.frame = CGRectMake((_mywidth-signLabelWidth)/2.0, _nickNameLabel.frame.origin.y+_nickNameLabel.frame.size.height+8, signLabelWidth, 24);
    _signLabel.layer.cornerRadius = 8.0f;
    _signLabel.layer.borderColor = TabBarBjSelectColor.CGColor;
    _signLabel.layer.borderWidth = 1.0f;
    _signLabel.layer.masksToBounds = YES;
    self.bottomView.frame = CGRectMake(0, _myheight-60, _mywidth, 60);
    CGFloat bottomHeight = 60;
    CGFloat btnWidth = _mywidth/3.0;
    CGFloat btnHeight = 30;
    CGFloat labelHeight = 21;
    CGFloat btnStartHeight = 6;
    CGFloat labelStartHeight = 60-25;
    self.jifenBtn.frame = CGRectMake(0, btnStartHeight, btnWidth, btnHeight);
    self.jifenLabel.frame = CGRectMake(0, labelStartHeight, btnWidth, labelHeight);
    self.firstSXimageView.frame = CGRectMake(btnWidth, 10, 1, bottomHeight-20);
    
    self.dengjiBtn.frame = CGRectMake(btnWidth, btnStartHeight, btnWidth, btnHeight);
    self.dengjiLabel.frame = CGRectMake(btnWidth, labelStartHeight, btnWidth, labelHeight);
    self.secondSXimageView.frame = CGRectMake(2*btnWidth, 10, 1, bottomHeight-20);
    self.jingbiBtn.frame = CGRectMake(btnWidth*2, btnStartHeight, btnWidth, btnHeight);
    self.jingbiLabel.frame = CGRectMake(btnWidth*2, labelStartHeight, btnWidth, labelHeight);
    
    UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeBGphoto)];
    single.numberOfTapsRequired = 1;
    _bgImageView.userInteractionEnabled = YES;
    [_bgImageView addGestureRecognizer:single];
    
    
}

-(void)changeBGphoto
{
    CDLog(@"改变背景图片");
    
    [self upLoadPhotoHandle:@"更改背景" andtag:1000];
    
}
-(void)upLoadPhotoHandle:(NSString *)title andtag:(int)tag
{
    _uploadtag = tag;
    [[UIActionSheet bk_actionSheetCustomWithTitle:title buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index)
      {
          [self photoActionSheet:sheet DismissWithButtonIndex:index];
          
      }] showInView:self];
}

- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        // 拍照
        if (![Helper checkCameraAuthorizationStatus])
        {
            return;
        }
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = (id)self;
        picker.allowsEditing = NO;//设置是否可编辑
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self.VC presentViewController:picker animated:YES completion:nil];//进入照相界面
    }
    else if (buttonIndex == 1)
    {
        // 相册
        if (![Helper checkPhotoLibraryAuthorizationStatus])
        {
            return;
        }
        
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = (id)self;
        picker.allowsEditing = NO;//设置是否可编辑
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.VC presentViewController:picker animated:YES completion:nil];
    }
}

//- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    if (buttonIndex ==1)
//    {
//        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//        //        picker.allowsEditing = YES;//有可编辑的框
//        picker.delegate = (id)self;
//        //[self UploadPhoto:fileName];
//        [self presentViewController:picker animated:YES completion:NULL];
//    }
//    else if(buttonIndex ==0)
//    {
//        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
//        {
//            
//            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//            NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
//            picker.mediaTypes = temp_MediaTypes;
//            picker.delegate = (id)self;
//            //picker.allowsEditing = YES;
//            //            picker.allowsEditing = YES;//有可编辑的框
//            
//        }
//        //[self UploadPhoto:_fileName];
//        [self presentViewController:picker animated:YES completion:^{}];
//        
//        
//    }
//}


#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *pickerImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary writeImageToSavedPhotosAlbum:[pickerImage CGImage] orientation:(ALAssetOrientation)pickerImage.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error)
     {
         UIImage *myImage = [self scaleImage:pickerImage toScale:1.0];
         //然后把这个图片上传到服务器上
         
     }];
    
    [picker dismissViewControllerAnimated:YES completion:^{}];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}



-(void)setCellData:(id)data
{
    

}

- (IBAction)btnClickedHandle:(UIButton *)sender
{
    
    switch (sender.tag) {
        case 100:
        {
            CDLog(@"上传头像ing");
            if ([LoginHandle isHadLogin:self.VC]) {
                
                [self upLoadPhotoHandle:@"上传头像" andtag:10001];
            }
            
        }
            break;
        case 101:
        {
            CDLog(@"签到");
            if ([LoginHandle isHadLogin:self.VC]) {
                
                [self upLoadPhotoHandle:@"上传头像" andtag:10001];
            }
            
        }
            break;
            
        default:
            break;
    }
    
}

- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize

{
    UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize, image.size.height * scaleSize));
    [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)];
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the view for the selected state
}

@end
